package org.spider.webkit.core.test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.TimeoutException;

import org.apache.zookeeper.KeeperException;
import org.junit.Test;

import com.gome.spiderkit.client.CookieJar;
import com.gome.spiderkit.client.GlobalConfig;
import com.gome.spiderkit.client.Rect;
import com.gome.spiderkit.client.UnknowServerException;
import com.gome.spiderkit.client.WebElement;
import com.gome.spiderkit.client.WebKit;
import com.gome.spiderkit.client.WebPage;
import com.google.protobuf.InvalidProtocolBufferException;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;


public class testSpider {
	@Test
	public void downloadTest() throws KeeperException, InterruptedException, IOException, UnknowServerException, TimeoutException
	{
		init();
		WebKit webkit = new WebKit("test");

		WebPage page = webkit.get("http://guide.qyer.com/melbourne/");
		if(page == null)
		{
			System.out.println("getWebPage error!");
			return;
		}
		
		CookieJar jar = page.getCookieJar();

		System.out.println(page.getUrl()+page.getTitle());

		System.out.println("下载需要的Cookie: " + jar.toString());

		String login_user_css = "#qyer_head_username";
		if (page.waitForWebElement(login_user_css, 1000)) {
			WebElement element = page.getElementByCssSelector(login_user_css);
			System.out.println(element.getOuterXml());
		}
		
		String resource_css = ".down.jsdownaction";
		if (page.waitForWebElement(resource_css, 1000)) {
			WebElement element = page.getElementByCssSelector(resource_css);
			System.out.println(element.getOuterXml());
			String link = page.download(element, 30000);
			System.out.println("下载链接: " + link);
		}
		page.destory();
		webkit.release();
	}
	
	@Test
	public void testCookie() throws IOException, InterruptedException, KeeperException, UnknowServerException, TimeoutException
	{
		init();
		
		WebKit webkit = new WebKit("test");
		
		WebPage page = webkit.get("http://www.jd.com");
		if(page == null)
		{
			System.out.println("getWebPage error!");
			return;
		}
		
		CookieJar jar = page.getCookieJar();
		
		System.out.println(page.getUrl()+page.getTitle());
		
		System.out.println(jar.toJson());
		
		page.destory();
		webkit.release();
	}
	
	public void write(String path, String content) {
			try {
			File f = new File(path);
			if (f.exists()) {
				System.out.println("文件存在");
			} else {
				System.out.println("文件不存在，正在创建...");
				if (f.createNewFile()) {
					System.out.println("文件创建成功！");
				} else {
					System.out.println("文件创建失败！");
					return;
				}
			}
			BufferedWriter output = new BufferedWriter(new FileWriter(f));
			output.write(content);
			output.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
    public String getLocalIP() {                                                                                                                                                                      
        Enumeration<NetworkInterface> netInterfaces = null;
        String ip = "127.0.0.1";
        ArrayList<BasicDBObject> items = new ArrayList<BasicDBObject>();
        try {
            netInterfaces = NetworkInterface.getNetworkInterfaces();
            while (netInterfaces.hasMoreElements()) {
                NetworkInterface ni = netInterfaces.nextElement();
                String name = ni.getName().toLowerCase();
                if (name.startsWith("lo") || name.startsWith("vir") || name.startsWith("vmnet") || name.startsWith("wlan")) {
                    continue;
                }
                Enumeration<InetAddress> ips = ni.getInetAddresses();
                while (ips.hasMoreElements()) {
                    InetAddress ia = ips.nextElement();
                    if (ia instanceof Inet4Address) {
                        if (ia.getHostAddress().toString().startsWith("127")) {
                            continue;
                        } else {
                            ip = ia.getHostAddress();
                            BasicDBObject bs = new BasicDBObject();
                            bs.append("name",  name);
                            bs.append("ip", ip);
                            items.add(bs);
                        }
                    }
                }
            }
        } catch (Exception e) {
        }

        Collections.sort(items, new Comparator<BasicDBObject>() {

            public int compare(BasicDBObject o1, BasicDBObject o2) {
                return o1.getString("name").compareToIgnoreCase(o2.getString("name"));
            }
        });
        if (items.size() > 0) {
            ip = items.get(0).getString("ip");
        }
        return ip; 
    }  
    
    @Test
    public void printTitle() throws KeeperException, InterruptedException, IOException, UnknowServerException, TimeoutException {
    	init();
    	WebKit webkit = new WebKit("test");
    	
		WebPage page = webkit.get("http://www.baidu.com");
		if(page == null)
		{
			System.out.println("getWebPage error!");
			return;
		}
		
		System.out.println(page.getTitle());
		
		page.destory();
		webkit.release();
    }
    
    @Test
    public void testScroll() throws KeeperException, InterruptedException, IOException, UnknowServerException, TimeoutException {
    	init();
    	WebKit webkit = new WebKit("test");
    	
		WebPage page = webkit.get("http://product.suning.com/108645587.html");
		if(page == null)
		{
			System.out.println("getWebPage error!");
			return;
		}
		System.out.println(page.getTitle());
		ArrayList<WebElement> list = page.getElementsByCssSelector("#productDetail img");
		for (WebElement e : list) {
			Rect r = e.getRect();
			page.scroll(r.getX(), r.getY() + 5);
			System.out.println(e.getAttribute("src"));
		}
		page.destory();
		webkit.release();
    }
    
    @Test
	public void getProtocol() throws InterruptedException, KeeperException, IOException {
		String zkhost = "10.58.222.103:2181";
		GlobalConfig config = GlobalConfig.getInstance();
		config.connect(zkhost);
		String protocol = config.getProtocolFromGroup("test", false);
		System.out.println(protocol);
	}
    
    @Test
    public void meituanItems() throws InterruptedException, IOException, UnknowServerException, TimeoutException {
		init();
		
    	WebKit webkit = new WebKit("test");
    	
    	String url = "http://bj.meituan.com/category/pc_ktv?mtt=1.index%2Ffloor.nc.12.i6u3kv54";
    	
		WebPage page = webkit.get(url);
		if(page == null)
		{
			System.out.println("getWebPage error!");
			return;
		}
		
		System.out.println(page.getTitle());
		
		Thread.sleep(1000);
		
		int contentCount = 0;
		List<WebElement> items = page.getElementsByCssSelector(".deal-tile");
		contentCount = items.size();
		
		int newCount = 0;
		do {
			System.out.println("count : " + contentCount);
			for (int i =0 ;i < items.size(); i ++) {
				Rect r = items.get(i).getRect();
				System.out.println("x: " + r.getX() + " y : " + r.getY());
				page.scroll(r.getX() + 5, r.getY() + 5);
			}
			
			Thread.sleep(1000);
			items = page.getElementsByCssSelector(".deal-tile");
			newCount = items.size();
			System.out.println("new count : " + newCount);
			if (newCount == contentCount)
				break;
			else
				contentCount = newCount;
			System.out.println("newcount : " + contentCount);
		} while (true);
		
		System.out.println(items.size());
		
		Thread.sleep(200);
		
		for (WebElement element : items) {
			Rect r = element.getRect();
			page.scroll(r.getX() + 5, r.getY() + 5);
			Thread.sleep(200);
			List<WebElement> linkEs = element.getChildsByCssSelector(".w-link");
			if (linkEs.size() <= 0) {
				//System.out.println(element.getInnerXml());
				System.out.println("Link null !");
				continue;
			}
			String href = linkEs.get(0).getAttribute("href");
			String text = linkEs.get(0).getAttribute("title");
			System.out.println(text + " | " + href);
		}
		
		page.destory();
		webkit.release();
    }
    
    public void init() throws IOException, InterruptedException {
		String zkhost = "10.58.222.103:2181,10.58.222.104:2181";
		GlobalConfig config = GlobalConfig.getInstance();
		config.connect(zkhost);
    }

}